<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<%@ taglib uri="/WEB-INF/tld/sys.tld" prefix="sys" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="renderer" content="webkit">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>项目报价方案</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>

<script type="text/javascript">
	var prjId='${prjId}';
	var buOrgCd = '${buOrgCd}';
	var insCode = '${insCode}';
	var grid = null;
	var yesOrNoCdListJSON = [];
	var detailFlag='${detailFlag}';
	var isFlow = '${isFlow}';
	var isShowAdd = '${isShowAdd}';
    <c:if test="${not empty yesOrNoCdListJSON}"> yesOrNoCdListJSON = ${yesOrNoCdListJSON}; </c:if>
	$(function () {
		initGrid();//grid
		if(detailFlag=="true"){
			$("#addBtn").hide();
			/* $("#removeBtn").hide(); */
			$("#splitPaySchBtn").hide();
		}
		if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_XNY&&isShowAdd==1&&detailFlag=="true"){
			$("#addBtn").show();
		}
		
	});
	
	//初始化列表
	function initGrid(){
		var  flag = true;
		if(detailFlag=="true"){
			flag = false;
		}
		var params = {};
		params.prjId = prjId;
		params.quatTypCd = ProjectConstant.CODE_PAY_QUAT_PRPS_OFFICIAL;
		$("#grid").ligerGrid({
	    	url: '<%=path%>/project/prjquatprps/initList.do',
	    	method : 'get',
	    	parms:params,
	        columns: [
	            { display: '方案名称', name: 'prpsCfgNm', minWidth: 140,
	            	render: function (rowdata, rowindex, value, column) { 
	            		var h = '';
	            		    h='<a  href="javascript:detailBtn(';
	                        h+="'"+rowdata.id+"',";
	                        h+="'"+rowdata.payWayCd+"',";
                            h+="'"+rowdata.isLowBiz+"'";
	                        h+=')">'+value+'</a>';
	                        return h;
	            		}	
	            },
	            { display: '租赁总价值', name: 'totLsItmAmt', minWidth: 140 ,render:renderAmt,enabledSort:true},
	            { display: '合同额', name: 'contAmt', minWidth: 140,render:renderAmt,enabledSort:true},
	            { display: '租赁期数', name: 'lsPd', minWidth: 60 , enabledSort:true},
	            { display: '支付方式', name: 'payWayCdNm', minWidth: 100 },
	            { display: '方案类型', name: 'prpsTypCdNm', minWidth: 100 },
	            
	            {display: '是否为正式报价方案', name: 'isFormalPrps', minWidth: 80,
	            	editor: {
	                    type: 'select',
	                    data: yesOrNoCdListJSON,
	                    valueField: 'key',
	                    textField: 'value'
	                },
	                valueField: 'isFormalPrps',
	                textField: 'isFormalPrpsNm',
	                render: function (item) {
	                    return item.isFormalPrpsNm;
	                }
	            },
	            
	            { display: '保证金处理方式', name: 'dpstHdlWayCdNm', minWidth: 60 },
	            { display: '期满处理方式', name: 'expHdlWayCdNm', minWidth: 60},
	            { display: '实际收益率(%)', name: 'irrPct', minWidth: 100},
	            { display: '实际收益率2(%)', name: 'irrPct2', minWidth: 100},
	            { display: '创建时间', name: 'crtTm', minWidth: 60, render:renderDate},
	            {
	                display: '操作',
	                isSort: false,
	                width: 150,
	                render: function (rowdata, rowindex, value){
	                	 var h="";
	                	 if(detailFlag!="true"){
		                	// if(rowdata.prpsTypCd==ProjectConstant.CODE_PAY_QUAT_PRPS_QUOTE){
		                		<sys:FlowFrmAth preTaskIds="${preTaskIds}" athCd="addPrjBscInfo" pInstId="${pInstId}">
			                        h+='<a class="sq-button-hasicon" href="javascript:modifyBtn(';
			                        h+="'"+rowdata.quatId+"',";
			                        h+="'"+rowdata.payWayCd+"',";
                                    h+="'"+rowdata.isLowBiz+"'";
			                        h+=')"><i class="sqBtnIco_4"></i>修改</a>';
			                        h+='<a class="sq-button-hasicon" href="javascript:removeQuatBtn(';
			                        h+="'"+rowdata.prjId+"'";
			                        h+=')"><i class="sqBtnIco_4"></i>删除</a>';
								</sys:FlowFrmAth>
		                	// }
	               		 }
	                /* 	 if(rowdata.prpsTypCd==ProjectConstant.CODE_PAY_QUAT_PRPS_QUOTE&&rowdata.isFormalPrps==CommonConstant.C_YN_Y){
	                		//生成报价版支付表、生成报价版支付表、录入版信息
	                		
	                		  h+='<a class="sq-button-hasicon" href="javascript:splitQuatPaySchBtn(';
		                       h+="'"+rowdata.id+"'";
		                       h+=')"><i class="sqBtnIco_4"></i>生成客户版支付表</a>';
		                       
		                       if(rowdata.payWayCd==ProjectConstant.CODE_BEGI_EQUAL__PRINC_INTER||rowdata.payWayCd==ProjectConstant.CODE_END_EQUAL__PRINC_INTER||ProjectConstant.CODE_MULTI__STEP_INTER){
		                        h+='<a class="sq-button-hasicon" href="javascript:genNomalPaySchBtn(';
		                        h+="'"+rowdata.id+"'";
		                        h+=')"><i class="sqBtnIco_4"></i>生成系统版信息</a>';
		                       }
	                	 } 
	                         h+='<a class="sq-button-hasicon" href="javascript:detailBtn(';
	                        h+="'"+rowdata.id+"'";
	                        h+=')">详细</a>' */
	                    return h;
	                }
	            }
	        ],
	        onBeforeSubmitEdit: function (e) {//重新赋值,要不然选中后离开会把key显示在前端
                if (e.selected && e.column.columnname == "isFormalPrpsNm") {
                    e.record.isFormalPrpsId = e.value;
                    e.value = e.selected[e.column.editor.textField];
                }
            },
	        pageSize:100,
	        root:'rows',
	        record:'total',
	        hideLoadButton : true,
	        width: '100%',
	        height:'100%',
	        heightDiff:-2,
	        checkbox:true,
	        allowUnSelectRow: true,
            alternatingRow: true,
	        rownumbers:true,
	        enabledEdit: flag,
	        usePagerOnLessPagesize: true,
	         onAfterEdit: f_onAfterEdit,
	    });
		grid = liger.get("grid");
		grid.setHeight(300);
		
		 //编辑后事件 
        function f_onAfterEdit(e)
        {
            if (e.column.name == "isFormalPrps")
            {
            	var val = e.record.isFormalPrps;//获取值
            	//值为是默认方案
            		//判断grid中默认值的数量>1提示
            		 var grid = $("#grid").ligerGetGridManager();
                     var data = grid.getData();
                     var  num = 0;
            	 	for(var i=0;i<data.length;i++){
            			if(data[i].isFormalPrps=="1"&&data[i].quatTypCd==ProjectConstant.CODE_PAY_QUAT_PRPS_OFFICIAL){
            				num+=1;
            			}
            		} 
             		if(num==0){
						 top.$.ligerDialog.SQalert({content: "请设置一个默认的一个正式报价方案！",SQ_type: "error"});
						 return;
           			}
             		
            	/* 	if(num>1){
						 top.$.ligerDialog.SQalert({content: "该项目只能有一个正式报价方案！",SQ_type: "error"});
						 return;
            		}  */
            	//	if(num==1){
            		//	for(var i=0;i<data.length;i++){
                			//if(data[i].isFormalPrps=="1"){
                    				//修改方案为默认方案
                    				var val = e.record.isFormalPrps;//获取值
                    				var params = {};
                    				params.prjId = prjId;
                    				params.isFormalPrps = val;
                    				params.id=e.record.id;
                    				setIsFormalPrpsStatus(params);
                		//	}
                	//	}
            //		}
            	
            }
        } 
	}
	//格式化为人民币格式
	function renderAmt(rowdata, rowindex, value) {
        var h="￥" + FormatMoney(value)
        return h;
    }

	function setIsFormalPrpsStatus(params){
		if(params.prjId){
			//查询是否有支付表信息
			$.ajax({
				type: 'get',
				dataType:'json',
				data:params,
				url: '<%=path%>/project/prjprdpayschm/getPrjPrdPayByPrjId.do',
				success:function(msg){
					 if(msg.data){
							$.ligerDialog.confirm("已经存在支付表信息，需要修改默认方案、删除已经拆分的支付表信息吗？", function (flag) {
								if(flag == false)return;
								$.ajax({
									type: 'post',
									dataType:'json',
									data:params,
									url: '<%=path%>/project/prjquatprps/modifyIsFormalPrpsStatus.do',
									success:function(msg){
										 if(msg.success == 1){
											 top.$.ligerDialog.SQalert({content: "修改成功！",SQ_type: "success"});
											 grid.reload();
										 }else{
											 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
										 }
									}
								});
								
							});
					 }else{
							$.ajax({
								type: 'post',
								dataType:'json',
								data:params,
								url: '<%=path%>/project/prjquatprps/modifyIsFormalPrpsStatus.do',
								success:function(msg){
									 if(msg.success == 1){
										 top.$.ligerDialog.SQalert({content: "修改成功！",SQ_type: "success"});
										 grid.reload();
									 }else{
										 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
									 }
								}
							});
					 }
				}
			});
		}
		
	}
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value&&value!=''){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
		}
		return dateStr;
	}
	//添加
	function addBtn(){
		var callback = function(routeFlag,closeFlag,isBankShow,fundRasInsId,fundRasInsNm,insCode,title){
			 grid.reload();
			 if(routeFlag == CommonConstant.C_YN_Y){
			     if(closeFlag == CommonConstant.C_YN_Y){
                     var callback=GET_W2Wcallback();
                     if(callback) callback();
                     var TAB=top.mainApp.parentTab;
                     var currentTabid=TAB.getSelectedTabItemID();
                     top.tab.removeTabItem(currentTabid);
                 }else{
                     var selectid= parent.$("#isInsBiz").ligerGetComboBoxManager();
                     if(fundRasInsId != null){
                         selectid.setValue(1);
                         selectid.setText("是");
                         parent.$("#isInsBiz").val(1);
                         parent.$("#fundRasInsNmDiv").show();
                         parent.$("#fundRasInsNm").html(fundRasInsNm);
                         parent.$("#fundRasInsId").val(fundRasInsId);
                         parent.$("#insCode").val(insCode);
                         parent.insCodeCheck = insCode;
                         if(ProjectConstant.PRJ_INS_CODE_ICBC == insCode){
                             var isHaveUpStruct = parent.$("#isHaveUpStruct").ligerGetComboBoxManager();
                             isHaveUpStruct.setValue(0);
                             isHaveUpStruct.setText("否");
                             parent.$("#isHaveUpStructDiv").show();
                         }else{
                             parent.$("#isHaveUpStructDiv").hide();
                         }
                         if (CommonConstant.C_YN_Y == isBankShow){
                             if(parent.pageInTab.isTabItemExist("tabM")) {
                                 parent.pageInTab.removeTabItem("tabM");
                             }
                             parent.pageInTab.addTabItem({
                                 text: title,
                                 tabid: 'tabM',
                                 showClose: false,
                                 url: '<%=path%>/project/prjBscInfoInsDistribute/toPrjBscInsEditPage.do?prjId='+prjId,
                             });
                         }else{
                             parent.pageInTab.removeTabItem("tabM");
                         }
                     }else{
                         selectid.setValue(0);
                         selectid.setText("否");
                         parent.$("#isInsBiz").val(0);
                         parent.$("#fundRasInsNmDiv").hide();
                         parent.$("#fundRasInsNm").html("");
                         parent.$("#fundRasInsId").val("");
                         parent.$("#insCode").val("");
                         parent.insCodeCheck = "";
                         parent.$("#isHaveUpStructDiv").hide();
                         parent.pageInTab.removeTabItem("tabM");
                     }
                 }
             }
		}
        $.ligerDialog.SQopen({title: '选择方案', height: 400,width: 1000, url: '<%=path%>/project/prjquatprps/toPrjQuatPrpsCfgMPage.do?prjId='+prjId, isResize: true ,callback:callback});
	}

	//修改
    function modifyBtn(id,payWayCd,isLowBiz){
        var callback = function(){
            grid.reload();
        }
        if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_CAR||buOrgCd==ProjectConstant.CODE_BU_ORG_CD_ZC){
            if(payWayCd==ProjectConstant.CODE_MULTI__STEP_INTER_GH){
                Add_navTab('tab_prj_quat_prps'+id,'修改报价方案','<%=path%>/project/prjquatprps/toAddCarBankPage.do?id='+id+'&&prjId='+prjId,callback);
            }else{
                if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_ZC&&insCode&&insCode!='1'){
                    Add_navTab('tab_prj_quat_prps'+id,'修改报价方案','<%=path%>/project/prjquatprps/toAddPage.do?prjId='+prjId+'&&id='+id+'&&isFlow='+isFlow+'&&isLowBiz='+isLowBiz,callback);
                }else{
                    Add_navTab('tab_prj_quat_prps'+id,'修改报价方案','<%=path%>/project/prjquatprps/toAddCarPage.do?id='+id+'&&prjId='+prjId+'&&isLowBiz='+isLowBiz,callback);
                }
            }
        }else if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_XNY){
            Add_navTab('tab_prj_quat_prps'+id,'修改报价方案','<%=path%>/project/prjquatprps/toAddNewEngeryPage.do?id='+id+'&&prjId='+prjId+'&&isLowBiz='+isLowBiz,callback);
        }else if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_ZK&&insCode&&insCode==ProjectConstant.PRJ_INS_CODE_ICBC){
            Add_navTab('tab_prj_quat_prps','修改报价方案','<%=path%>/project/prjquatprps/toAddZKBankPage.do?id='+id+'&&prjId='+prjId,callback);
        }else{
            Add_navTab('tab_prj_quat_prps'+id,'修改报价方案','<%=path%>/project/prjquatprps/toAddPage.do?prjId='+prjId+'&&id='+id+'&&isFlow='+isFlow+'&&isLowBiz='+isLowBiz,callback);
        }
    }
	
	//查看
    function detailBtn(id,payWayCd,isLowBiz){
        var editFlag = true;
        if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_CAR||buOrgCd==ProjectConstant.CODE_BU_ORG_CD_ZC){
            if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_ZC&&insCode&&insCode!='1'){
                Add_navTab('tab_prj_quat_prps'+id,'查看报价方案','<%=path%>/project/prjquatprps/toAddPage.do?prjId='+prjId+'&&id='+id+'&&detailFlag='+editFlag+'&&isLowBiz='+isLowBiz);
            }else{
                Add_navTab('tab_prj_quat_prps'+id,'查看报价方案','<%=path%>/project/prjquatprps/toCarDetailPage.do?prjId='+prjId+'&&id='+id+'&&detailFlag='+editFlag+'&&insCode='+insCode+'&&isLowBiz='+isLowBiz);
            }
        }else if(buOrgCd==ProjectConstant.CODE_BU_ORG_CD_ZK&&insCode&&insCode==ProjectConstant.PRJ_INS_CODE_ICBC){
            Add_navTab('tab_prj_quat_prps','查看报价方案','<%=path%>/project/prjquatprps/toAddZKBankPage.do?id='+id+'&&prjId='+prjId+'&&detailFlag='+editFlag);
        }else{
            Add_navTab('tab_prj_quat_prps'+id,'查看报价方案','<%=path%>/project/prjquatprps/toAddPage.do?prjId='+prjId+'&&id='+id+'&&detailFlag='+editFlag+'&&isLowBiz='+isLowBiz);
        }
    }

	function iframeResize () {
	    if (parent.mainApp) {
	        parent.mainApp.parentTab.onResize();
	    };
	}
	//删除所选信息
	function removeBtn(id){
		var _ids = [];
/* 		var selectedRows = grid.getSelectedRows();
		if(selectedRows.length <1){
			$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
			return;
		}
		for ( var index in selectedRows) {
			_ids.push(selectedRows[index].id);
		} */
		_ids.push(id);
		$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
			if(flag == false)return;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/project/prjquatprps/removeByIds.do',
				success:function(msg){
					 if(msg.success == 1){
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
		});
		
	}
	
	//拆分支付表
	function splitPaySch(){
		var flag = false;
		//检查是否有项目报价方案
		var quatTypCd = ProjectConstant.CODE_PAY_QUAT_PRPS_OFFICIAL;
		var params = {};
		      params.prjId=prjId;
		      params.quatTypCd=quatTypCd;
			$.ajax({
				type: 'get',
				dataType:'json',
				data:params,
				url: '<%=path%>/project/prjquatprps/initList.do',
				success:function(msg){
					 if(msg.total >= 1){
							//拆分项目产品及项目支付表
							/* var selectedRows = grid.getSelectedRows();
							if(selectedRows.length <1){
								$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
								return;
							} */
							var prdNm='';							
							var _ids = [];
							//for ( var index in selectedRows) {
								//查找产品支付表是否存在
								$.ajax({
									type: 'get',
									async: false,
									dataType:'json',
									data:{prjId:prjId},
									url: '<%=path%>/project/prjprddtl/initList.do',
									success:function(msg){
											if(msg.rows){
												for(var i=0;i<msg.rows.length;i++){
													var obj = msg.rows[i];
													if(obj.paySchNo){
														flag = true;
														break;
													}
												}
											}
										}
									})
							//	_ids.push(selectedRows[index].id);
						//	}
							var params = {};
							params.prjPrdIdList=JSON.stringify(_ids);
							params.prjId=prjId;
							if(flag){
							$.ligerDialog.confirm("支付表已经存在，确定要重新拆分吗？", function (flag) {	
							if(flag == false)return;
							splitPrjPrdAndPaySch(params);
						});		
					}else{
						 splitPrjPrdAndPaySch(params);
					}
					 }else{
						 top.$.ligerDialog.SQalert({content: "请先配置项目报价方案！",SQ_type: "error"});
					 }
				}
			});

	}
	function splitPrjPrdAndPaySch(params){
		var wait_dialog;
		$.ajax({
			type: 'post',
			dataType:'json',
			data:params,
			beforeSend: function(){
		          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
		     },
			url: '<%=path%>	/project/prjprddtl/splitPrjPrdAndPaySch.do',
			success:function(msg){
				 if(msg.success == 1){
					 wait_dialog.close();
					 top.$.ligerDialog.SQalert({content: "拆分成功！",SQ_type: "success"});
				 }else{
					 wait_dialog.close();
					 top.$.ligerDialog.SQalert({content: "拆分失败！",SQ_type: "error"});
				 }
			}
		});
	}
	
	function splitQuatPaySchBtn(id){
		var wait_dialog;
		$.ajax({
			type: 'post',
			dataType:'json',
			data:{id:id},
			beforeSend: function(){
		          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
		     },
			url: '<%=path%>	/project/prjquatprps/splitQuatPaySch.do',
			success:function(msg){
				 if(msg.success == 1){
					 wait_dialog.close();
					 top.$.ligerDialog.SQalert({content: "拆分成功！",SQ_type: "success"});
				 }else{
					 wait_dialog.close();
					 top.$.ligerDialog.SQalert({content: "拆分失败！",SQ_type: "error"});
				 }
			}
		});
		
	}
	
	function genNomalPaySchBtn(id){
		//生成录入版数据
		var wait_dialog;
		$.ajax({
			type: 'post',
			dataType:'json',
			data:{id:id},
			beforeSend: function(){
		          wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
		     },
			url: '<%=path%>	/project/prjquatprps/genNomalPaySch.do',
			success:function(msg){
				 if(msg.success == 1){
					 wait_dialog.close();
					 top.$.ligerDialog.SQalert({content: "操作成功！",SQ_type: "success"});
					 grid.reload();
				 }else{
					 wait_dialog.close();
					 top.$.ligerDialog.SQalert({content: "操作失败！",SQ_type: "error"});
				 }
			}
		});
		
	}
	// 删除报价版方案、录入版方案、录入版支付表
	function removeQuatBtn(prjId){
			$.ligerDialog.confirm(LogUtils.LOG_CMM_DELETE_CONFIRM, function (flag) {
				if(flag == false)return;
				$.ajax({
					type: 'post',
					dataType:'json',
					data:{prjId:prjId},
					url: '<%=path%>/project/prjquatprps/removeByPrjId.do',
					success:function(msg){
						 if(msg.success == 1){
							 grid.reload();
						 }else{
							 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
						 }
					}
				});
			});
	}
	
	
</script>

<body class="iframeBody">
	<div class="Wrapper">
      <div id="pageInTab" class="pageInTab">
      <div class="searchArea">
    <form id="searchBar">
       <div class="fieldset1 clearFix">
<div class="btnsBox">
	<sys:FlowFrmAth preTaskIds="${preTaskIds}" athCd="addPrjBscInfo" pInstId="${pInstId}">
         <span id="addBtn"  onclick="addBtn();"  class="sq-button sq-button-round" >新增</span>
	</sys:FlowFrmAth>
     <!--     <span id="removeBtn" onclick="removeBtn();" class="sq-button sq-button-round">作废</span> -->
        <!--   <span id="modifyBtn" onclick="modifyBtn();" class="sq-button sq-button-hasicon"><i class="sqBtnIco_3"></i>修改</span> -->
  <!--   <span id="splitPaySchBtn" onclick="splitPaySch();" class="sq-button sq-button-round">生成支付表</span> -->
  </div>
  </div>
</form>
</div>
<div id="grid" class="sq-grid"></div>
</div>
		</div>
</body>
</html>